﻿using System.Xml;
using System.Xml.Linq;

namespace _48_C_生成Xml文件的三种方式_第二种使用Linq_To_Xml_
{
    internal class Program
    {
        static void Main(string[] args)
        {
            XDocument doc = new XDocument();
            XDeclaration xDeclaration = new XDeclaration("1.0", "utf-8", null);
            doc.Declaration = xDeclaration;

            XElement studentInfo = new XElement("StudentInfo");
            doc.Add(studentInfo);

            XElement student = new XElement("Student");
            studentInfo.Add(student);
            student.SetAttributeValue("Grade", "一年级");
            student.SetAttributeValue("Teacher", "张老师");

            XElement name = new XElement("name");
            name.Value = "张三";
            student.Add(name);

            XElement age = new XElement("age");
            age.Value = "18";
            student.Add(age);

            doc.Save("D:\\test2.xml");
        }
    }
}
